﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ProgramCat.FiddlerExtension
{
    public class V8Help : Singleton<V8Help>
    {
        private V8Help() { }

        /// <summary>
        /// V8引擎错误返回汉化
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public V8Analysis Analysis(string str)
        {
            V8Analysis v8Analysis = new V8Analysis();
            string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            try
            {
                if (strs.Length >= 2)
                {
                    //汉化行1
                    strs[0] = strs[0].Replace("Uncaught ReferenceError", "引用错误");
                    strs[0] = strs[0].Replace("is not defined", "未定义");
                    strs[0] = strs[0].Replace("Uncaught SyntaxError", "语法错误");
                    strs[0] = strs[0].Replace("Unexpected token", "意外的符号");
                    strs[0] = strs[0].Replace("Uncaught TypeError", "类型错误");
                    strs[0] = strs[0].Replace("is not a function", "不是函数");
                    strs[0] = strs[0].Replace("Cannot read property", "无法读取属性");
                    strs[0] = strs[0].Replace("of null", "空");
                    v8Analysis.data = strs[0];
                    //汉化
                    string[] strs1 = strs[1].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    v8Analysis.line = int.Parse(strs1[1]);
                    v8Analysis.column = int.Parse(strs1[3]);
                    v8Analysis.data += "\r\n" + string.Format("第 {0} 行,第 {1} 个字符处", strs1[1], strs1[3]);
                }
            }
            catch {
                v8Analysis.data = strs[0] + "\r\n" + strs[1];
            }
            return v8Analysis;
        }
    }
    public class V8Analysis
    {
        //Uncaught ReferenceError: webpackJsonp is not defined
        //Line: 1  Column: 0
        //Stack: 
        //  at V8.NET:1:1
        public int line = -1;
        public int column = -1;
        public string data = "";
    }

}
